package com.base.taskreports.common.aspect;

import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.base.taskreports.common.utlis.DateUtil;
import com.base.taskreports.common.utlis.DateUtils;
import com.base.taskreports.demoExel.entity.Indicator;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.base.taskreports.common.annotation.TBatchLog;
import com.base.taskreports.reports.entiyt.master.TBatchEntity;
import com.base.taskreports.reports.service.TBatchService;
import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

/**
 * 接口日志，切面处理类
 */
@Aspect
@Component
@Slf4j
public class TBatchLogAspect {

    private static final Map<String, String> taskMap;
    static
    {
        taskMap = new HashMap<String, String>();
        taskMap.put("LJ", "立减");
        taskMap.put("PQL", "票券-领取");
        taskMap.put("PQD", "票券-承兑");
        taskMap.put("JFS", "积分-送分");
        taskMap.put("JFU", "积分-用分");
    }

    @Autowired
    private TBatchService tBatchService;

    @Pointcut("@annotation(com.base.taskreports.common.annotation.TBatchLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {

        //第一步：将请求报文存入数据库表中
        TBatchEntity saveTBatchLogLogRequestMsg = saveTBatchLogLogRequestMsg(point);

        // 执行方法
        Object result = point.proceed();

        // 保存日志
        saveTBatchLogLogResponseMsg(saveTBatchLogLogRequestMsg);
        return result;
    }

    //保存请求报文
    private TBatchEntity saveTBatchLogLogRequestMsg(ProceedingJoinPoint joinPoint) throws ParseException {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        TBatchLog TBatchLogLog = method.getAnnotation(TBatchLog.class);
        //获取注解上的描述
        String TBatchLogMethod = TBatchLogLog.value();

        Object[] args = joinPoint.getArgs();

        Date taskDate ;

        if(args.length == 0){
            taskDate = DateUtils.transformatDate(DateUtil.getToday());
        }else {
        	if(args[0]!=null) {
        		taskDate = DateUtils.transformatDate(args[0].toString()) ;
        	}else {
        		taskDate = DateUtils.transformatDate(DateUtil.getToday());
        	}
        	
            
        }

        TBatchEntity tBatchEntity = new TBatchEntity();
        tBatchEntity.setTaskId(TBatchLogMethod);
        tBatchEntity.setTaskName(Indicator.valueOf(TBatchLogMethod).getName());
        tBatchEntity.setTaskDate(taskDate);
        tBatchEntity.setStartTime(new Date());
        tBatchEntity.setCronExpression("");
        tBatchEntity.setIsFinished("0");
        tBatchService.saveTBatch(tBatchEntity);
        return tBatchEntity;
    }

    //更新响应报文
    private void saveTBatchLogLogResponseMsg(TBatchEntity saveTBatchLogLogRequestMsg) {
        saveTBatchLogLogRequestMsg.setEndTime(new Date());
        saveTBatchLogLogRequestMsg.setIsFinished("1");
        tBatchService.updateTBatch(saveTBatchLogLogRequestMsg);
    }
}
